<?php defined('SYSPATH') OR die('No direct access allowed.');

class Submission_Model {

    public $token;
    private static $repository_path;
    private $submit_content;
    private $grade_content;
    public $problem;
    public $submit_time;
    public $loaded = FALSE;

    public function  __construct($token = NULL) {
        $this->token = $token;
        $this->submit_content = array();
        $this->grade_content = array();
        if ($this->token != NULL)
        {
            $path = self::get_repository_path()."/".$this->token;
            if (file_exists($path))
            {
                $array = json::readfile($path);
                if (!is_null($array))
                {
                    $this->problem = new Problem_Model($array['problem']);
                    $this->submit_time = $array['submit_time'];
                    $this->submit_content = $array['submit_content'];
                    $this->grade_content = $array['grade_content'];
                    $this->loaded = TRUE;
                }
            }
        }
    }

    public static function get_repository_path()
    {
        self::$repository_path = Kohana::config('evaluator.submission_repository_path');
        return self::$repository_path;
    }

    /**
     *
     * @param string $name
     * @return object
     */
    public function get_submit_content($name)
    {
        return ((isset($this->submit_content[$name])) ? $this->submit_content[$name] : NULL);
    }

    /**
     *
     * @param string $name
     * @param object $value
     */
    public function set_submit_content($name, $value)
    {
        $this->submit_content[$name] = $value;
    }

    /**
     *
     * @param string $name
     * @return object
     */
    public function get_grade_content($name)
    {
        return ((isset($this->grade_content[$name])) ? $this->grade_content[$name] : NULL);
    }

    /**
     *
     * @param string $name
     * @param object $value
     */
    public function set_grade_content($name, $value)
    {
        $this->grade_content[$name] = $value;
    }

    /**
     *
     */
    public function save()
    {
        if (is_null($this->token))
        {
            $path = tempnam(self::get_repository_path(), "");
            $this->token = basename($path);
        }
        else
        {
            $path = self::get_repository_path()."/".$this->token;
        }
        $array = array();
        $array['problem'] = $this->problem->token;
        $array['submit_time'] = $this->submit_time;
        $array['submit_content'] = $this->submit_content;
        $array['grade_content'] = $this->grade_content;
        json::writefile($array, $path, TRUE);
        $this->loaded = TRUE;
    }

    public function delete()
    {
        if (!is_null($this->token))
        {
            $path = self::get_repository_path()."/".$this->token;
            unlink($path);
        }
    }

    public static function get_list()
    {
        $path = self::get_repository_path();
        $submissions = array();
        $dh = opendir($path);
        while (($file = readdir($dh)) !== false) {
            if ($file == '.' || $file == '..')  continue;
            $absolutepath = $path."/".$file;
            if (filetype($absolutepath) != 'dir')
            {
                if (!is_null(json::readfile($absolutepath)))
                {
                    $submission = new Submission_Model($file);
                    $submissions[] = $submission;
                }
            }
        }
        closedir($dh);
        return $submissions;
    }

}
//end of file